package com.jereh.topnews.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.HorizontalScrollView;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.jereh.topnews.R;
import com.jereh.topnews.activity.NewsMainActivity;
import com.jereh.topnews.adapter.FragmentDemoPagerAdapter;
import com.jereh.topnews.entity.Channel;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/11/3.
 */
public class NewsListFragment extends Fragment {

    private RadioGroup group;
    private ViewPager pager;
    private HorizontalScrollView scrollView;
    private FragmentDemoPagerAdapter adapter;
    private Context ctx;
    private List<Channel> list=new ArrayList<>();
    private List<Fragment> fragments=new ArrayList<>();
    private Channel[] channels;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.ctx=context;

    }

    private void initChannel() {
//        Channel shehui=new Channel(R.id.shehui,"社会");
//        Channel yule=new Channel(R.id.yule,"娱乐");
//        Channel guoji=new Channel(R.id.guoji,"国际");
        if(list.isEmpty()) {
            list.add(channels[0]);
            list.add(channels[1]);
            list.add(channels[2]);
            list.add(channels[3]);
            list.add(channels[4]);
        }
    }

    private void addChannel(LayoutInflater inflater,RadioGroup group,int i) {
        View btn=inflater.inflate(R.layout.radio_btn_layout,null);
        RadioButton channel=(RadioButton)btn.findViewById(R.id.radio);
        channel.setText(list.get(i).getName());
        channel.setId(i);
        group.addView(channel);
    }

    private void addFragment(Channel channel) {
        //实例化fragment时，传递参数
        NewsItemFragment itemFragment=new NewsItemFragment();
        //通过setArguments传递参数
        Bundle bundle=new Bundle();
        bundle.putSerializable("channel",channel);
        itemFragment.setArguments(bundle);
        fragments.add(itemFragment);
    }
    private void setGroupItem(int i) {
        //一个按钮的宽度
        int width=group.getChildAt(0).getWidth();
        //设置横向滚动滚动到的位置
        //x:x轴坐标，y:y轴坐标
        scrollView.smoothScrollTo(width * i, 0);
    }

    private void initChannelList(){
        if(getActivity() instanceof  NewsMainActivity) {
            //添加到频道list中
            List<Channel> tlist=((NewsMainActivity)getActivity()).list;
            channels=tlist.toArray(new Channel[tlist.size()]);
        }
    }

    @Nullable
    @Override
    public View onCreateView(final LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        initChannelList();
        initChannel();
        final View view=inflater.inflate(R.layout.fragment_news_list,null);
        scrollView=(HorizontalScrollView)view.findViewById(R.id.scrollView);
        //动态添加按钮
        group=(RadioGroup)view.findViewById(R.id.group);
        if(group.getChildCount()==0) {
            for (int i = 0; i < list.size(); i++) {
                addChannel(inflater, group, i);
            }
        }
        ((RadioButton)group.getChildAt(0)).setChecked(true);
        if(fragments.isEmpty()) {
            for (int i = 0; i < list.size(); i++) {
                addFragment(list.get(i));
            }
        }
        pager=(ViewPager)view.findViewById(R.id.newsList);
        adapter=new FragmentDemoPagerAdapter(getChildFragmentManager(),fragments);
        pager.setAdapter(adapter);
        group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                pager.setCurrentItem(checkedId);
                setGroupItem(checkedId);
            }
        });
        pager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
                ((RadioButton) group.getChildAt(position)).setChecked(true);
                setGroupItem(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        view.findViewById(R.id.add).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (list.size() < channels.length) {
                    list.add(channels[list.size()]);
                    addChannel(inflater, group, list.size() - 1);
                    addFragment(list.get(list.size()-1));
                    adapter.notifyDataSetChanged();
                }
            }
        });
        return view;

    }
}
